package com.alibaba.nacos.plugin.datasource.impl.kingbase8;

import com.alibaba.nacos.common.utils.CollectionUtils;
import com.alibaba.nacos.plugin.datasource.constants.TableConstant;
import com.alibaba.nacos.plugin.datasource.mapper.GroupCapacityMapper;
import com.alibaba.nacos.plugin.datasource.model.MapperContext;
import com.alibaba.nacos.plugin.datasource.model.MapperResult;
import com.alibaba.nacos.plugin.datasource.constants.FieldConstant;


import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

import static com.alibaba.nacos.plugin.datasource.constants.DefaultConstant.DEFAULT_TENANT_ID;

public class GroupCapacityMapperByKingbase8 extends AbstractKingbase8Mapper implements GroupCapacityMapper {


    @Override
    public String getTableName() {
        return TableConstant.GROUP_CAPACITY;
    }

    @Override
    public String getFunction(String s) {
        return s;
    }


    @Override
    public MapperResult insertIntoSelect(MapperContext context) {


        List<Object> paramList = new ArrayList();

        paramList.add(context.getUpdateParameter(FieldConstant.GROUP_ID));
        paramList.add(context.getUpdateParameter(FieldConstant.QUOTA));

        paramList.add(context.getUpdateParameter(FieldConstant.MAX_SIZE));
        paramList.add(context.getUpdateParameter(FieldConstant.MAX_AGGR_COUNT));
        paramList.add(context.getUpdateParameter(FieldConstant.MAX_AGGR_SIZE));
        paramList.add(LocalDateTime.now());
        paramList.add(LocalDateTime.now());



        return new MapperResult("INSERT INTO group_capacity (group_id, quota, `usage`, `max_size`, max_aggr_count, max_aggr_size,gmt_create,"
                + " gmt_modified) SELECT ?, ?, count(*), ?, ?, ?, ?, ? FROM config_info", paramList);
    }

    @Override
    public MapperResult insertIntoSelectByWhere(MapperContext context) {
        List<Object> paramList = new ArrayList();

        paramList.add(context.getUpdateParameter(FieldConstant.GROUP_ID));
        paramList.add(context.getUpdateParameter(FieldConstant.QUOTA));

        paramList.add(context.getUpdateParameter(FieldConstant.MAX_SIZE));
        paramList.add(context.getUpdateParameter(FieldConstant.MAX_AGGR_COUNT));
        paramList.add(context.getUpdateParameter(FieldConstant.MAX_AGGR_SIZE));
        paramList.add(LocalDateTime.now());
        paramList.add(LocalDateTime.now());
        paramList.add(context.getUpdateParameter(FieldConstant.GROUP_ID));
        return new MapperResult("INSERT INTO group_capacity (group_id, quota,`usage`, `max_size`, max_aggr_count, max_aggr_size, gmt_create,"
                + " gmt_modified) SELECT ?, ?, count(*), ?, ?, ?, ?, ? FROM config_info WHERE group_id=? AND tenant_id = '" + DEFAULT_TENANT_ID + "'", paramList);
    }

    @Override
    public MapperResult incrementUsageByWhereQuotaEqualZero(MapperContext context) {
        List<Object> paramList = new ArrayList();
        paramList.add(LocalDateTime.now());
        paramList.add(context.getUpdateParameter(FieldConstant.GROUP_ID));
        paramList.add(context.getUpdateParameter(FieldConstant.USAGE));
        return new MapperResult("  group_capacity SET `usage` = `usage` + 1, gmt_modified = ? WHERE group_id = ? AND `usage` < ? AND quota = 0",paramList);
    }

    @Override
    public MapperResult incrementUsageByWhereQuotaNotEqualZero(MapperContext context) {
        List<Object> paramList = new ArrayList();
        paramList.add(LocalDateTime.now());
        paramList.add(context.getUpdateParameter(FieldConstant.GROUP_ID));
        return new MapperResult("UPDATE group_capacity SET `usage` = `usage` + 1, gmt_modified = ? WHERE group_id = ? AND `usage` < quota AND quota != 0",paramList);
    }

    @Override
    public MapperResult incrementUsageByWhere(MapperContext context) {
        List<Object> paramList = new ArrayList();
        paramList.add(LocalDateTime.now());
        paramList.add(context.getUpdateParameter(FieldConstant.GROUP_ID));
        return new MapperResult("UPDATE group_capacity SET `usage` = `usage` + 1, gmt_modified = ? WHERE group_id = ?", paramList);
    }

    @Override
    public MapperResult decrementUsageByWhere(MapperContext context) {
        List<Object> paramList = new ArrayList();
        paramList.add(LocalDateTime.now());
        paramList.add(context.getUpdateParameter(FieldConstant.GROUP_ID));
            return new MapperResult("UPDATE group_capacity SET `usage` = `usage` - 1, gmt_modified = ? WHERE group_id = ? AND `usage` > 0", paramList);
    }

    @Override
    public MapperResult updateUsage(MapperContext context) {
        List<Object> paramList = new ArrayList();
        paramList.add(LocalDateTime.now());
        paramList.add(context.getUpdateParameter(FieldConstant.GROUP_ID));
        return new MapperResult("UPDATE group_capacity SET `usage` = (SELECT count(*) FROM config_info), gmt_modified = ? WHERE group_id = ?",paramList);
    }

    @Override
    public MapperResult updateUsageByWhere(MapperContext context) {
        List<Object> paramList = new ArrayList();
        paramList.add(context.getUpdateParameter(FieldConstant.GROUP_ID));
        paramList.add(LocalDateTime.now());
        paramList.add(context.getUpdateParameter(FieldConstant.GROUP_ID));
        return new MapperResult("UPDATE group_capacity SET `usage` = (SELECT count(*) FROM config_info WHERE group_id=? AND tenant_id = '" + DEFAULT_TENANT_ID + "'),"
                + " gmt_modified = ? WHERE group_id= ?",paramList);
    }

    @Override
    public MapperResult selectGroupInfoBySize(MapperContext context) {
        List<Object> paramList = new ArrayList();
        paramList.add(context.getWhereParameter(FieldConstant.ID));
        return new MapperResult(getDatabaseDialect().getLimitTopSqlWithMark("SELECT id, group_id FROM group_capacity WHERE id > ? "),paramList);
    }


}

